<template>
  <div class="system-crmTalent-container layout-padding">
    <el-card shadow="hover" class="layout-padding-auto">
      <div class="system-crmTalent-search">
        <el-form :inline="true" ref="crmTalentSearchRef" :model="state.tableData.param.query" size="default">
          <el-row>
            <!-- <el-form-item label="申请日期" class="ml20" prop="applyDate">
              <el-date-picker clearable
                              v-model="state.tableData.param.query.applyDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="选择申请日期">
              </el-date-picker>
            </el-form-item> -->
            <el-form-item label="姓名" class="ml20" size="default">
              <el-input v-model="state.tableData.param.query.talentName" placeholder="请输入姓名" clearable
                      style="max-width: 180px"></el-input>
            </el-form-item>
            <!-- <el-form-item label="人才性别" class="ml20" size="default">
              <el-input v-model="state.tableData.param.query.talentGender" placeholder="请输入人才性别" clearable
                      style="max-width: 180px"></el-input>
            </el-form-item> -->
            <el-form-item label="手机号码" class="ml20" size="default">
              <el-input v-model="state.tableData.param.query.talentPhone" placeholder="请输入手机号码" clearable
                      style="max-width: 180px"></el-input>
            </el-form-item>
            <!-- <el-form-item label="民族" class="ml20" size="default">
              <el-input v-model="state.tableData.param.query.talentNationality" placeholder="请输入民族" clearable
                      style="max-width: 180px"></el-input>
            </el-form-item>
            <el-form-item label="政治面貌" class="ml20" size="default">
              <el-input v-model="state.tableData.param.query.talentPolitical" placeholder="请输入政治面貌" clearable
                      style="max-width: 180px"></el-input>
            </el-form-item>
            <el-form-item label="婚姻状况" class="ml20" size="default">
              <el-input v-model="state.tableData.param.query.talentMarrige" placeholder="请输入婚姻状况" clearable
                      style="max-width: 180px"></el-input>
            </el-form-item> -->
            <el-form-item label="证件号码" class="ml20" size="default">
              <el-input v-model="state.tableData.param.query.talentIdenno" placeholder="请输入证件号码" clearable
                      style="max-width: 180px"></el-input>
            </el-form-item>
            <!-- <el-form-item label="户籍所在地" class="ml20" size="default">
              <el-input v-model="state.tableData.param.query.talentRegistration" placeholder="请输入户籍所在地" clearable
                      style="max-width: 180px"></el-input>
            </el-form-item>
            <el-form-item label="现居住地" class="ml20" size="default">
              <el-input v-model="state.tableData.param.query.talentResidential" placeholder="请输入现居住地" clearable
                      style="max-width: 180px"></el-input>
            </el-form-item>
            <el-form-item label="国内最高学历" class="ml20" size="default">
              <el-input v-model="state.tableData.param.query.domesticHighestDegree" placeholder="请输入国内最高学历" clearable
                      style="max-width: 180px"></el-input>
            </el-form-item>
            <el-form-item label="国内最高学位" class="ml20" size="default">
              <el-input v-model="state.tableData.param.query.domesticHighestDiploma" placeholder="请输入国内最高学位" clearable
                      style="max-width: 180px"></el-input>
            </el-form-item>
            <el-form-item label="照片路径" class="ml20" size="default">
              <el-input v-model="state.tableData.param.query.photoPath" placeholder="请输入照片路径" clearable
                      style="max-width: 180px"></el-input>
            </el-form-item>
            <el-form-item label="登录密码" class="ml20" size="default">
              <el-input v-model="state.tableData.param.query.loginPass" placeholder="请输入登录密码" clearable
                      style="max-width: 180px"></el-input>
            </el-form-item> -->
            <el-button size="default" type="primary" class="ml20" @click="getTableData">
              <el-icon><ele-Search /></el-icon> 查询
            </el-button>
            <el-button size="default" class="ml20" @click="resetQuery">
              <el-icon><ele-Refresh /></el-icon> 重置
            </el-button>
          </el-row>
        </el-form>
      </div>
      <el-row :gutter="35">
          <el-button size="default" type="success" plain class="ml30" @click="onOpenAdd('add')">
            <el-icon><ele-FolderAdd /></el-icon> 新增
          </el-button>
      </el-row>
      <el-table :data="state.tableData.records" v-loading="state.tableData.loading" style="width: 100%">
        <el-table-column type="index" label="序号" width="60" />
        <el-table-column label="人才类型" prop="talentType" show-overflow-tooltip>
          <template #default="scope">
          <span>{{ talent_type[scope.row.talentType].label }}</span>
          </template>
        </el-table-column>
        <el-table-column label="业务类型" prop="busiType" show-overflow-tooltip>
          <template #default="scope">
            <span>{{ busi_type[scope.row.busiType].label }}</span>
          </template>
        </el-table-column>
        <el-table-column label="业务办理进度" prop="progressType" show-overflow-tooltip>
        <template #default="scope">
            <span>{{ progress_type[scope.row.progressType].label }}</span>
          </template>
        </el-table-column>
        <el-table-column label="申请日期" prop="applyDate" show-overflow-tooltip>
          <template #default="scope">
            <span>{{ parseDateTime(scope.row.applyDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="姓名" prop="talentName" show-overflow-tooltip/>
        <el-table-column label="性别" prop="talentGender" show-overflow-tooltip>
        <template #default="scope">
            <span>{{ sys_user_sex[scope.row.talentGender].label }}</span>
          </template>
        </el-table-column>
        <el-table-column label="手机号码" prop="talentPhone" show-overflow-tooltip/>
       
        <el-table-column label="证件号码" prop="talentIdenno" show-overflow-tooltip/>
 
         
        <el-table-column label="照片" prop="photoPath" show-overflow-tooltip>
        <template #default="scope">
          <el-image 
              :src="scope.row.photoPath" 
              fit="cover"
              style="width: 60px; height: 60px; border-radius: 6px;"
              :preview-src-list="[scope.row.photoPath]"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100">
          <template #default="scope">
            <el-button size="small" text type="primary" @click="onOpenEdit('edit', scope.row)">修改</el-button>
            <el-button size="small" text type="primary" @click="onRowDel(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          @size-change="onHandleSizeChange"
          @current-change="onHandleCurrentChange"
          class="mt15"
          :pager-count="5"
          :page-sizes="[10, 20, 30]"
          v-model:current-page="state.tableData.param.page.current"
          background
          v-model:page-size="state.tableData.param.page.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="state.tableData.total"
      >
      </el-pagination>
    </el-card>
    <CrmTalentDialog ref="crmTalentDialogRef" @refresh="getTableData()" />
  </div>
</template>

<script setup lang="ts" name="crmTalent">
  import { defineAsyncComponent, reactive, onMounted, ref, nextTick, getCurrentInstance } from 'vue';
  import { ElMessageBox, ElMessage } from 'element-plus';
  import { useCrmTalentApi } from '/@/api/system/talent';
  import { parseDateTime } from '/@/utils/formatTime';
  
  // 引入组件
  const CrmTalentDialog = defineAsyncComponent(() => import('./dialog.vue'));
  const { proxy } = getCurrentInstance();
  const { sys_user_sex,talent_type,busi_type,progress_type,talent_nationality,talent_political,talent_marrige,domestic_highest_degree,domestic_audit_status,domestic_highest_diploma } = proxy.parseDict("talent_type","sys_user_sex",
         "busi_type","progress_type","talent_nationality","talent_political","talent_marrige","domestic_highest_degree","domestic_audit_status","domestic_highest_diploma");
  // 定义变量内容
  const crmTalentDialogRef = ref();
  const crmTalentSearchRef = ref();
  const useCrmTalent = useCrmTalentApi();
  const state = reactive({
    tableData: {
      records: [],
      total: 0,
      loading: false,
      param: {
        page: {
          current: 1,
          size: 10,
        },
        query: {
          talentType: '',    
          busiType: '',    
          progressType: '',    
          applyDate: '',    
          talentName: '',    
          talentGender: '',    
          talentPhone: '',    
          talentNationality: '',    
          talentPolitical: '',    
          talentMarrige: '',    
          talentIdenno: '',    
          talentRegistration: '',    
          talentResidential: '',    
          domesticHighestDegree: '',    
          domesticAuditStatus: '',    
          domesticHighestDiploma: '',    
          photoPath: '',    
          loginPass: '',    
        },
      },
    },
  });

  // 初始化表格数据
  const getTableData = () => {
    state.tableData.loading = true;
    useCrmTalent.getPageList(state.tableData.param).then(res => {
      state.tableData.records = res.records;
      state.tableData.total = res.total;
    });
    setTimeout(() => {
      state.tableData.loading = false;
    }, 500);
  };
  // 打开新增人才弹窗
  const onOpenAdd = (type: string) => {
    crmTalentDialogRef.value.openDialog(type);
  };
  // 打开修改人才弹窗
  const onOpenEdit = (type: string, row) => {
    crmTalentDialogRef.value.openDialog(type, row);
  };
  // 删除人才
  const onRowDel = (row) => {
    ElMessageBox.confirm('此操作将永久删除数据，是否继续?', '提示', {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    })
        .then(() => {
          useCrmTalent.deleteCrmTalent(row.id).then(() => {
            getTableData();
            ElMessage.success('删除成功');
          });
        })
        .catch(() => {
          ElMessage.error('删除失败');
        });
  };
  // 重置搜索框
  const resetQuery = () => {
    nextTick(() => {
        crmTalentSearchRef.value.resetFields();
    });
  }
  // 分页改变
  const onHandleSizeChange = (val: number) => {
    state.tableData.param.page.size = val;
    getTableData();
  };
  // 分页改变
  const onHandleCurrentChange = (val: number) => {
    state.tableData.param.page.current = val;
    getTableData();
  };
  // 页面加载时
  onMounted(() => {
    getTableData();
  });
</script>

<style scoped lang="scss">
  .system-crmTalent-container {
    :deep(.el-card__body) {
      display: flex;
      flex-direction: column;
      flex: 1;
      overflow: auto;
      .el-table {
        flex: 1;
      }
    }
  }
</style>
